package com.m3958.firstgwt.client.datasource;
import java.util.LinkedHashMap;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.constants.WebSiteFieldConstants;
import com.m3958.firstgwt.client.types.FlagPermission;
import com.m3958.firstgwt.client.types.WebSiteField;
import com.m3958.firstgwt.client.utils.FormValidatorUtils;
import com.m3958.firstgwt.shared.type.CommonField;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceEnumField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.FieldType;


@Singleton
public class WebSiteDataSource extends BaseDataSource{
   
    public static String className = "com.m3958.firstgwt.server.model.WebSite";

    @Inject
    public WebSiteDataSource(DsErrorHandler deh,WebSiteFieldConstants constants) {
    	setID("webSiteDS");
        DataSourceTextField cnameField = new DataSourceTextField(WebSiteField.CNAME.getValue(),constants.cname());
        cnameField.setRequired(true);
        
        DataSourceTextField enameField = new DataSourceTextField(WebSiteField.ENAME.getValue(),constants.ename());
        enameField.setRequired(true);
        enameField.setValidators(FormValidatorUtils.getSiteNameValidator());
        DataSourceTextField titleField = new DataSourceTextField(WebSiteField.TITLE.getValue(),constants.title());
        DataSourceEnumField flagPermissionField = new DataSourceEnumField(WebSiteField.FLAG_PERMISSION.getValue(),constants.flagPermission());
        flagPermissionField.setType(FieldType.ENUM);
        flagPermissionField.setRequired(true);
        flagPermissionField.setValueMap(FlagPermission.SITE_AUDITOR.toString(),FlagPermission.SECTION_AUDITOR.toString());
        DataSourceTextField displayAuthorsField = new DataSourceTextField(WebSiteField.DISPLAY_AUTHORS.getValue(),constants.displayAuthors());
        DataSourceTextField articleSourcesField = new DataSourceTextField(WebSiteField.ARTICLE_SOURCES.getValue(),constants.articleSources());
        DataSourceTextField articleFlagsField = new DataSourceTextField(WebSiteField.ARTICLE_FLAGS.getValue(),constants.articleFlags());
        DataSourceTextField imgSizeField = new DataSourceTextField(WebSiteField.IMG_SIZES.getValue(),constants.imgSizes());
        DataSourceTextField logoUrlField = new DataSourceTextField(WebSiteField.LOGO_URL.getValue(),constants.logoUrl());
        DataSourceTextField editorCssField = new DataSourceTextField(WebSiteField.EDITOR_CSS.getValue(),constants.richEditorCssPath());
        DataSourceBooleanField cacheEnableField = new DataSourceBooleanField(WebSiteField.CACHE_ENABLE.getValue(),constants.cacheEnable());
        DataSourceBooleanField stopField = new DataSourceBooleanField(WebSiteField.STOP.getValue(),constants.stop());
        DataSourceBooleanField sfurlField = new DataSourceBooleanField(WebSiteField.SEARCH_FRIEND_URL.getValue(),constants.searchFriendUrl());
        DataSourceEnumField commentLevelField = new DataSourceEnumField(CommonField.COMMENT_LEVEL.getValue(), constants.commentLevel());
        DataSourceTextField metakeywordField = new DataSourceTextField(WebSiteField.META_KEYWORDS.getValue(),constants.metaKeywords());
        DataSourceTextField metadescriptionField = new DataSourceTextField(WebSiteField.META_DESCRIPTION.getValue(),constants.metaDescription());
        DataSourceIntegerField rssDateNumField = new DataSourceIntegerField(WebSiteField.RSS_DATE_NUM.getValue(), constants.rssDateNum());
        LinkedHashMap<String, String> vm = new LinkedHashMap<String, String>();
        vm.put("0", "不允许");
        vm.put("1", "无需审核");
        vm.put("2", "允许，审核");
        vm.put("3", "允许，登录");
        vm.put("4", "允许，登录，审核");
        commentLevelField.setValueMap(vm);
        
        DataSourceEnumField tplTechField = new DataSourceEnumField(WebSiteField.TPL_TECH.getValue(),constants.tplTech());
        
        LinkedHashMap<String, String> tplvm = new LinkedHashMap<String, String>();
        tplvm.put("ftl", "FreeMarker");
        tplvm.put("vm", "Velocity");
        tplTechField.setValueMap(tplvm);
        
        DataSourceBooleanField commentClosedField  = new DataSourceBooleanField(WebSiteField.COMMENT_CLOSED.getValue(),constants.commentClosed());
        addHandleErrorHandler(deh);

        setFields(getIdField(),cnameField,enameField,getCreatedAtField(false,constants),getVersionField(),titleField,logoUrlField,
        		imgSizeField,displayAuthorsField,articleSourcesField,
        		articleFlagsField,cacheEnableField,stopField,sfurlField,
        		getPerpageField(constants),commentLevelField,commentClosedField,
        		editorCssField,metakeywordField,metadescriptionField,rssDateNumField,flagPermissionField,tplTechField);
        setClientOnly(false);
       
    }

	@Override
	protected String getModelClassName() {
		return className;
	}
}
